<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller {
    public function index(){
           
            //实例化数据库
            $ss = D("user");

            $do = I("get.do");
          

             $flag=1;
            //判断是否进行登录操作
            if($do == 'chk'){
                //获取前端数据
                $user = I("post.user");
                $pass = md5(md5(I("post.pass")));
                $remember = I("post.remember");
                //用cookie保存数据
                if(!empty($remember)){
                    cookie("user","$user",7*24*60*60);
                    cookie("pass","$pass",7*24*60*60);

                    //数据和数据库里的数据作对比
                    $where = array(
                    'user' => cookie("user"),
                    'pass' => cookie("pass"),
                    );
                }else{
                    //数据和数据库里的数据作对比
                    $where = array(
                    'user' => $user,
                    'pass' => $pass,
                    );
                }
                $is = $ss->where($where)->find();
                //判断帐号密码是否正确
                
                
               
              
                if (!$is) {
                     $flag=0;   

                     $this->assign("flag",$flag);                   
                }else{
                    //使用session获取id和user的值
                    $flag=1;

                    session("id", $is['id']);
                    // echo session("id");die();
                    session("user","$user");
                    return $this->redirect("Home/Index/index");
                }
                
                    
                    
               
                
                
            }

            $this->assign("flag",$flag);
            $this->display();

            
        }
    }